package com.wteam.dragon.system.util;

import javax.imageio.stream.FileImageOutputStream;
import java.io.File;

/**
 * 字节数组工具类
 *
 * @author 陈晓辉
 * @date 2020/2/7 10:04
 */
public class ByteUtil {
    /**
     * 图片最小字节数
     */
    private final static int MIN_PICTURE_BYTE = 3;

    /**
     * 字节数组转化为图片
     *
     * @param data     字节数组
     * @param typePath 储存的包的名字
     * @return
     */
    public String byteToImage(byte[] data, String typePath) {
        if (data.length < MIN_PICTURE_BYTE) {
            return "该字节数组太小不能形成图片";
        }
        try {
            FileUtil.FilePath filePath = FileUtil.getPath(FileUtil.FileType.PICTURE, typePath);
            String path = filePath.getFullPath();
            String uploadFilePath = filePath.getVisitPath();
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            return uploadFilePath;
        } catch (Exception e) {
            System.out.println("Exception: " + e);
            e.printStackTrace();
        }
        return "该字节数组太小不能形成图片";
    }
}
